package com.turing.data.engine.utils;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @ClassName regexUtil
 * @Description: TODO
 * @Author: fz
 * @Date 2020/12/3
 * @Version V1.0
 **/
public class QJwlTableFilterSoftUtil {

    public static int MARK_NUM = 1; //  ^符号的最小成行的个数

    //无效表头
    public static Boolean IneffectiveTableHeaderAlias(String tableHeader) {


        if (tableHeader.contains("注释")) {

            return true;
        }

        if (tableHeader.contains("序号")) {

            return true;
        }

        if (tableHeader.contains("行次")) {

            return true;
        }

        if (tableHeader.contains("附注")) {

            return true;
        }

        if (tableHeader.contains("注降")) {

            return true;
        }

        return false;
    }

    //有效表头
    static boolean TableHeaderAlias(String tableHeader) {

        if (tableHeader.contains("项目")) {

            return true;
        }

        if (tableHeader.contains("项参目")) {

            return true;
        }

        if (tableHeader.contains("科目")) {

            return true;
        }

        if (tableHeader.contains("项E")) {

            return true;
        }

        if (tableHeader.contains("项")) {

            return true;
        }

        if (tableHeader.contains("顶目")) {

            return true;
        }

        return false;
    }

    //识别表头
    static boolean DistinguishTableHeader(String tableHeader, Integer type) {

        if (countSubString(tableHeader, "^") >= MARK_NUM) {

            if (tableHeader.contains("项目^")) {

                return true;
            }

            if (tableHeader.contains("项参目^")) {

                return true;
            }

            if (tableHeader.contains("科目^")) {

                return true;
            }

            if (tableHeader.contains("项E^")) {

                return true;
            }

            if (tableHeader.startsWith("项^")) {

                return true;
            }

            if (tableHeader.contains("顶目^")) {

                return true;
            }
        }

        return false;
    }

    //识别表名
    static Integer TableNameAlias(String tableName) {

        if (tableName.contains("资产负债表")) {

            return 1;
        }

        if (tableName.contains("利润表")) {

            return 2;
        }

        if (tableName.contains("现金流量表") || tableName.contains("现流表")) {

            return 3;
        }

        return 0;
    }

    //识别所有表名的要素
    static int DistinguishAllElement(String element, Integer tableType) {

        int markNum = countSubString(element, "^");
        if (markNum >= MARK_NUM) {

            if (tableType == 1) {

                if (element.trim().contains("货币资金")) {

                    return markNum;
                }

                if (element.trim().contains("应收票据")) {

                    return markNum;
                }

                if (element.trim().contains("应收账款")) {

                    return markNum;
                }

                if (element.trim().contains("预付款项")) {

                    return markNum;
                }

                if (element.trim().contains("流动资产合计")) {

                    return markNum;
                }

                if (element.trim().contains("固定资产合计")) {

                    return markNum;
                }

                if (element.trim().contains("固定资产")) {

                    return markNum;
                }

                if (element.trim().contains("短期借款")) {

                    return markNum;
                }

                if (element.trim().contains("应付账款")) {

                    return markNum;
                }

                if (element.trim().contains("流动负债合计")) {

                    return markNum;
                }

                if (element.trim().contains("所有者权益")) {

                    return markNum;
                }

                if (element.trim().contains("资本公积")) {

                    return markNum;
                }

                if (element.trim().contains("盈余公积")) {

                    return markNum;
                }

                if (element.trim().contains("资产总计")) {

                    return markNum;
                }
            }

            if (tableType == 2) {

                if (element.trim().contains("营业总收入")) {

                    return markNum;
                }

                if (element.trim().contains("营业收入")) {

                    return markNum;
                }


                if (element.trim().contains("营业总成本")) {

                    return markNum;
                }

                if (element.trim().contains("营业成本")) {

                    return markNum;
                }

                if (element.trim().contains("营业利润")) {

                    return markNum;
                }

                if (element.trim().contains("利润总额")) {

                    return markNum;
                }

                if (element.trim().contains("净利润")) {

                    return markNum;
                }

                if (element.trim().contains("综合收益总额")) {

                    return markNum;
                }
            }
            if (tableType == 3) {
                if (element.trim().contains("经营活动现金流入小计")) {

                    return markNum;
                }

                if (element.trim().contains("经营活动现金流出小计")) {

                    return markNum;
                }

                if (element.trim().contains("经营活动产生的现金流量净额")) {

                    return markNum;
                }

                if (element.trim().contains("投资活动现金流入小计")) {

                    return markNum;
                }

                if (element.trim().contains("投资活动现金流出小计")) {

                    return markNum;
                }

                if (element.trim().contains("投资活动产生的现金流量净额")) {

                    return markNum;
                }

                if (element.trim().contains("筹资活动现金流入小计")) {

                    return markNum;
                }

                if (element.trim().contains("筹资活动现金流出小计")) {

                    return markNum;
                }

                if (element.trim().contains("筹资活动产生的现金流量净额")) {

                    return markNum;
                }
            }
        }
        return 0;
    }

    static Boolean DistinguishEffectiveValue(String element) {

        element = element.trim();
        if (element.contains("期末") || element.contains("期末") || element.contains("期末") ||
                element.contains("本期") || element.contains("金额") || element.contains("余额") ||
                element.contains("累计") || (element.contains("年") && element.contains("月"))) {

            return true;
        }
        if (Pattern.matches("^(\\d{4})(-|\\/)(\\d{1,2})\\2(\\d{1,2})$", element)) {
            return true;
        }
        ;
        if (Pattern.matches("^(\\d{4})(.|\\/)(\\d{1,2})\\2(\\d{1,2})$", element)) {
            return true;
        }
        ;
        return false;
    }


    /*获取规则表头有效的索引： 项目:序号:年末余额:项目:序号:年末余额   转换成 项目:年末余额:项目:年末余额
    0:规则表头 1不规则表头
    */
    static Integer getHeaderEffectiveIndex(List<String> tableHeaderList, int index) {

        Integer valueIndex = 0;
        for (int i = index; i < tableHeaderList.size(); i++) {

            if (DistinguishEffectiveValue(tableHeaderList.get(i))) {
                return i;
            }
        }
        return valueIndex;
    }


    /*获取不规则表头有效的索引： 项目:序号:年末余额:项目:序号:年末余额   转换成 项目:年末余额:项目:年末余额
    0:规则表头 1不规则表头
    */
    static Map<Integer, Integer> getOtherHeaderEffectiveIndex(String tableHeader, int index) {

        return null;
    }

    // countSubString，来返回字符串出现的个数
    public static int countSubString(String string, String subString) {
        // 定义一个count来存放字符串出现的次数
        int count = 0;
        // 调用String类的indexOf(String str)方法，返回第一个相同字符串出现的下标
        while (string.indexOf(subString) != -1) {
            count++;
            int index = string.indexOf(subString);
            //将长的那个字符串做截取
            string = string.substring(index + 1);
        }
        return count;
    }

    //出现最多数字
    public static int getMaxCountNum(List<Integer> countList) {

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < countList.size(); i++) {
            if (map.containsKey(countList.get(i))) {
                int temp = map.get(countList.get(i));
                map.put(countList.get(i), temp + 1);
            } else {
                map.put(countList.get(i), 1);
            }
        }
        Collection<Integer> count = map.values();

        int maxCount = Collections.max(count);

        int maxnum = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (maxCount == entry.getValue()) {
                maxnum = entry.getKey();
            }

        }
        return maxnum;
    }

}
